var express = require('express');

var app = express();

var fs = require('fs');

var users = [];

var user = {
   "userNew" : {
      "name" : "mohit",
      "password" : "password4",
      "profession" : "teacher",
      "id": 1 
   }
};

var n = 1;

app.put('/add', (req, resp) => {
		user['userNew'].name = 'tom' + n;
		user['userNew'].id = n ++;
		newUser = JSON.parse(JSON.stringify(user['userNew']));
		users.push(newUser);
		resp.end(JSON.stringify(users));
});

app.get('/list', (req, resp) => {
	console.log('list users');
	resp.end(JSON.stringify(users));
});

app.get('/:id', (req, resp) => {
	var id = parseInt(req.params.id);
	console.log('query => ', id, users[id]);
	resp.end(JSON.stringify(users[id]));
});

app.delete('/:id', (req, resp) => {
	var id = parseInt(req.params.id);
	console.log(users.splice(id,1));
	console.log('delete user =>', id);
	resp.end('ok');
});

var server = app.listen(8080, () => {
	console.log('server start at 8080');
})
